package com.chb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.CheckItemMapper;
import com.chb.system.service.CheckItemService;
import com.chb.system.service.UserService;
import com.chb.system.vo.CheckItemVo;
import com.hos.entity.sys.CheckItem;
import com.hos.entity.sys.Role;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ：张家琪
 * @date ：Created in 2023/12/14 15:50
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Resource
    private CheckItemMapper checkItemMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserService userService;


    @Override
    public Page<CheckItem> findCheckItemByCond(CheckItemVo checkItemVo) {
        Page<CheckItem> page = new Page<>(checkItemVo.getPageNum(), checkItemVo.getPageSize());
        LambdaQueryWrapper<CheckItem> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(checkItemVo.getCheckItemName())) {
            wrapper.like(CheckItem::getCheckItemName, checkItemVo.getCheckItemName());
        }
        if (StringUtils.hasText(checkItemVo.getKeywords())) {
            wrapper.like(CheckItem::getKeywords, checkItemVo.getKeywords());
        }
        if (StringUtils.hasText(checkItemVo.getTypeId())) {
            wrapper.eq(CheckItem::getTypeId, checkItemVo.getTypeId());
        }
        if (StringUtils.hasText(checkItemVo.getStatus())) {
            wrapper.eq(CheckItem::getStatus, checkItemVo.getStatus());
        }
        return checkItemMapper.selectPage(page, wrapper);
    }

    @Override
    public List<CheckItem> selectAllCheckItem() {
        LambdaQueryWrapper<CheckItem> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq(CheckItem::getStatus, "0");
        return checkItemMapper.selectList(wrapper);
    }

    @Override
    public Integer addCheckItem(CheckItem checkItem) {
        // 创建时间
        checkItem.setCreateTime(new Date());
        return checkItemMapper.insert(checkItem);
    }

    @Override
    public Integer deleteCheckItemByIds(List<Long> ids) {
        return checkItemMapper.deleteBatchIds(ids);
    }

    @Override
    public CheckItem getCheckItemById(Long checkItemId) {
        return checkItemMapper.selectById(checkItemId);
    }

    @Override
    public Integer updateCheckItem(CheckItem checkItem) {
        checkItem.setUpdateBy(getUserByToken().getUserName());
        return checkItemMapper.updateById(checkItem);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private User getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        return userService.findUserByPhone(phone);
    }

}